// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css';
import axios from 'axios'
import store from './store'
import VueAxios from 'vue-axios'
// import Antd from 'ant-design-vue';
// import 'ant-design-vue/dist/antd.css';
import NProgress from 'nprogress'; //加载动画
import 'nprogress/nprogress.css';
NProgress.configure({     
  easing: 'ease',  // 动画方式    
  speed: 500,  // 递增进度条的速度    
  showSpinner: false, // 是否显示加载ico    
  trickleSpeed: 300, // 自动递增间隔    
  minimum: 0.08 ,// 初始化时的最小百分比
  
})

Vue.use(VueAxios, axios);
Vue.use(NProgress);
// Vue.use(Antd);
Vue.use(ElementUI);
axios.defaults.baseURL = 'http://127.0.0.1:8989/'; //http://15982078875.gnway.cc:8000 打包后使用
axios.defaults.timeout = 9999999; //设置超时时间，单位毫秒
axios.defaults.retry = 3; //设置全局请求次数
axios.defaults.retryDelay = 1000; //设置全局请求间隙
Vue.prototype.$axios = axios; //将axios绑定到vue的原型上
Vue.prototype.$store = store;


Vue.config.productionTip = false

//全局请求拦截器 请求头加入token
axios.interceptors.request.use(
  config => {
      if (localStorage.getItem('Authorization')) {
          config.headers.Authorization = localStorage.getItem('Authorization');
      }
      return config;
  });


// 全局axios拦截响应
axios.interceptors.response.use(
  res => {
      // console.log(res)
      if (res.data.code === 401) {
          localStorage.clear(); //删除所有
          return Promise.resolve(res)
      }
      return Promise.resolve(res);
  }, err => {
      // console.log(err)
      //console.log(err.code)
      if (err) {
          switch (err.code) {
              case 400:
                  err.message = '请求错误(400)';
                  break;
              case 401:
                  err.message = '未授权，请重新登录(401)';
                  break;
              case 403:
                  err.message = '拒绝访问(403)';
                  break;
              case 404:
                  err.message = '请求出错(404)';
                  break;
              case 408:
                  err.message = '请求超时(408)';
                  break;
              case 500:
                  err.message = '服务器错误(500)';
                  break;
              case 501:
                  err.message = '服务未实现(501)';
                  break;
              case 502:
                  err.message = '网络错误(502)';
                  break;
              case 503:
                  err.message = '服务不可用(503)';
                  break;
              case 504:
                  err.message = '网络请求出错(504)';
                  break;
              case 505:
                  err.message = 'HTTP版本不受支持(505)';
                  break;
              default:
                  err.message = `连接出错(${err.status})!`;
          }
      } else {
          err.message = '连接服务器失败!'
      }
      Message.error({ message: err.message })
      return Promise.reject(err);
  }
);


//当路由进入前
router.beforeEach((to, from , next) => {
  // 每次切换页面时，调用进度条
 NProgress.start();
 // 这个一定要加，没有next()页面不会跳转的
  next();
});
//当路由进入后：关闭进度条
router.afterEach(() => {  
 // 在即将进入新的页面组件前，关闭掉进度条
 NProgress.done()
})


/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  // store,
  render: h => h(App) //Es6
})
